package com.JavaNet.chapter2_io;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author : wushikai
 * <p>
 * date : 2021-04-13
 */
public class ByteArrayInputStreamDemo {

    /**
     * 数据准备
     *
     * */
    private  static byte[] createIntBytes( ){

        return createIntBytes(10);

    }


    private  static byte[] createIntBytes( int num){

        byte[] origin = new byte[num];
        for (int i = 0; i < origin.length; i++) {
            origin[i] = (byte) i;
        }

        return origin;

    }





    /**
     * 读取字节流
     *
     * */
    private  static void readBytes() throws IOException {


        byte[] origin = createIntBytes();
        byte[] input = new byte[origin.length];
        ByteArrayInputStream in = new ByteArrayInputStream(origin);


        /*书中源码*/
        for (int i = 0; i < input.length; i++) {

            int b = in.read();
            if (b < 0) {
                break;
            }
            input[i] = (byte)b;


        }
        System.out.println("请打断点查看值");

    }


    private  static void readString ( ) throws IOException {

        String  str = "Java 基本数据类型";

        byte[] origin = str.getBytes();


        InputStream in = new ByteArrayInputStream(origin);

        byte[] bucket = new byte[10] ; // 定义一个 水桶去装原始 数据;


        int start = 0 ;
        int len = bucket.length;  //

        while (start < len){

            start += in.read(origin, start, len - start);


        }


        byte[] dest = new  byte[origin.length];

        for (int i = 0; i < origin.length; i++) {

            dest[i] = (byte) in.read();

        }


        System.out.println("请打断点查看值");


    }



    public static void main(String[] args) throws IOException {


        readBytes( );

        readString();




    }


}
